package com.lwl.crm.web.filter;

import com.lwl.crm.settings.entity.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest res = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        //获取拦截的路径
        String path = res.getServletPath();
        if("/settings/user/login.do".equals(path) || "/login.html".equals(path)){
            filterChain.doFilter(res,resp);
        }else {
            HttpSession session = res.getSession();
            User user = (User) session.getAttribute("user");
            if(user == null) {
                resp.sendRedirect(res.getContextPath()+"/login.html");
            }{
                filterChain.doFilter(res,resp);
            }
        }
    }
}
